<template>
  <el-form-item :label="labelValue" prop="hospitalId">
    <el-select
      v-model="hospitalId"
      filterable
      clearable
      reserve-keyword
      :loading="loadingIns"
      :placeholder="placeHolderValue"
      @change="hospitalIdChange"
      :disabled="disabled"
    >
      <el-option
        v-for="item in hospitalList"
        :key="item.id"
        :label="item.name"
        :value="item.id"
      >
      </el-option>
    </el-select>
  </el-form-item>
</template>

<script>
import HospitalApi from "@/api/institution/hospital";
export default {
  // 父组件参数
  props: {
    site: {
      type: Number,
      default: 1 // 1.新增/修改  2.查询
    },
    disabled: {
      // 限制输入
      type: Boolean,
      default: false
    },
    id: {
      // type: Number,
      default: null
    },
  },
  data() {
    return {
      // id: "",
      hospitalId: null,
      loadingIns: false,
      labelValue: "",
      placeHolderValue: "",
      hospitalList: []
    };
  },
  watch: {
    id(val) {
      if (val) {
        this.hospitalId = parseInt(val);
      } else {
        this.hospitalId = null;
      }
    }
  },
  components: {},
  methods: {
    listHospital() {
      let _this = this;
      _this.loadingIns = true;
      let cond = {
        hospitalType: 1
      };
      HospitalApi.getHospitalNameList(cond)
        .then(res => {
          setTimeout(() => {
            _this.loadingIns = false;
            _this.hospitalList = res.data||[]
          }, 200);
        })
        .catch(error => {
          _this.hospitalList = [];
          console.error(error);
        });
    },
    hospitalIdChange(val) {
      this.$emit("hospitalId", val);
    },
    hospitalIdEmpty() {
      this.hospitalId = "";
    }
  },
  created() {
    this.listHospital();

    if (this.id) {
      this.hospitalId = parseInt(this.id);
    }
    if (this.site === 1) {
      this.labelValue = "医院名称";
      this.placeHolderValue = "请选择医院";
    } else if (this.site === 2) {
      this.labelValue = "";
      this.placeHolderValue = "医院名称";
    }
  }
};
</script>
